﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Easy_27_RemovingElements
{
    //27.移除元素
    class MainProject
    {

        static void Main(string[] args)
        {
            int[] nums = new int[]{ 3, 2, 2, 3};
            int count = RemoveElement(nums, 3);
            foreach (var item in nums)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Count = " + count);
            Console.ReadKey();
        }

        public static int RemoveElement(int[] nums, int val)
        {
            Queue<int> deletePos = new Queue<int>();
            int sameNum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] == val)
                {
                    deletePos.Enqueue(i);
                }
            }
            sameNum = deletePos.Count;
            for (int j = nums.Length-1; j >=0; j--)
            {
                if (nums[j]!= val)
                {
                    if (deletePos.Count>0)
                    {
                        int index = deletePos.Dequeue();
                        nums[index] = nums[j];
                        continue;
                    }
                    break;
                }
            }

            return nums.Length-sameNum;
        }
    }
}
